/**
 * Description:
 *
 * @author      Eden
 * @datetime    2016/12/10 11:01
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */

(function () {

    var api = {

        "getListsUrl": '/order/lists/getLists',
        "getDetailUrl": '/order/lists/getDetail',
        "getExpressUrl": '/order/lists/getExpress',
        "setExpress": '/order/lists/setExpress',
        "getExpressInfoUrl": '/order/lists/getExpressInfo'

    };

    sm.controller('admin-order-index-controller', ['$scope', '$http', 'Modal', function ($scope, $http, Modal) {

        $scope.lists = [];
        $scope.nextPage = false;

        $scope.page = {
            "status": '',
            "page": 1
        };

        $scope.orderDetail = {};
        $scope.express = [];
        $scope.defaultExp = {};
        $scope.expSetting = {
            'key': null,
            'orderId': 0,
            'expressNo': '',
            'expressName': '',
            'simpleName': ''
        };
        $scope.expError = '';
        $scope.expressResult = [];

        $scope.selects = {
            nowStatus: '',
            allStatus: [{
                'key': 'Pending',
                'value': '待发货'
            }, {
                'key': 'Dispatch',
                'value': '已发货'
            }, {
                'key': 'Complete',
                'value': '已完成'
            }, {
                'key': 'Unpaid',
                'value': '未支付'
            }, {
                'key': 'Returns',
                'value': '已退货'
            }, {
                'key': 'Refund',
                'value': '已退款'
            }]
        };


        // tab切换
        $scope.select = function (status) {
            if ($scope.page.status == status) {
                return false;
            }

            $scope.page.status = status;
            $scope.page.page = 1;
            $scope.lists = [];
            loadList();
        };

        // 加载更多
        $scope.loadMore = function(page) {
            if ($scope.nextPage) {
                $scope.page.page += 1;
            }

            loadList();
        };

        // 模态框
        $scope.alertModal = function ($index) {
            $('#orderDetail').modal();
            $scope.detail = {};
            getDetail($index);

        };

        // 设置快递配送
        $scope.editExpress = function (id, key) {
            $('#expressModal').modal();
            $scope.expError = '';
            $scope.expSetting = {};
            $scope.expressResult = [];
            $scope.defaultExp = $scope.express[0];
            $scope.expSetting.orderId = id;
            $scope.expSetting.key = key;
            $scope.expSetting.expressNo = $scope.lists[key]['expressNo'] || '';

            // 获取快递查询数据
            getExpressInfo($scope.expSetting.expressNo, $scope.lists[key]['expressSimpleName']);
        };

        function getExpressInfo(no, sname)
        {
            if (!no || !sname) {
                return false;
            }

            $http.get(api.getExpressInfoUrl, {
                params: {
                    'expressNo': no,
                    'simpleName': sname
                }
            }).success(function (res) {
                if (res.state) {
                    $scope.expressResult = res.data.data;
                } else {
                    $scope.expressResult = [];
                }
            });
        }

        // 选择快递
        $scope.selectExp = function ($index) {
            $scope.defaultExp = $scope.express[$index];
        };

        // 单号设置
        $scope.setExpress = function () {
            if ($scope.expSetting.expressNo == '') {
                return false;
            }

            if ($scope.expressResult && $scope.expressResult.length > 0) {
                $scope.expError = '快递设置已不能更改';
                return false;
            }

            $scope.expSetting.simpleName = $scope.defaultExp.simpleName;
            $scope.expSetting.expressName = $scope.defaultExp.expName;
            var key = $scope.expSetting.key;

            $http.post(api.setExpress, $scope.expSetting).success(function (res) {
                if (res.state) {
                    // 在列表中的位置
                    $scope.lists[key]['expressNo'] = $scope.expSetting.expressNo;
                    $scope.lists[key]['expressSimpleName'] = $scope.expSetting.simpleName;
                    $scope.lists[key]['expressName'] = $scope.expSetting.expressName;
                    $scope.lists[key]['formatStatus'] = '已发货';
                    $scope.lists[key]['status'] = 'Dispatch';
                    $('#expressModal').modal('hide');
                } else {
                    $scope.expError = res.error ? res.error : '设置失败';
                }
            }).error(function () {
                $scope.expError = res.error ? res.error : '设置失败';
            });
        };

        // 获取详情
        function getDetail(orderId) {
            if ($scope.orderDetail.orderId && orderId == $scope.orderDetail.orderId) {
                return false;
            }

            $http.get(api.getDetailUrl, {
                params: {
                    orderId: orderId
                }
            }).success(function (res) {
                $scope.detail = res.data;
            });

        }

        (function (){
            $http.get(api.getExpressUrl).success(function (res) {
                if (res.state) {
                    $scope.express = res.data;
                    $scope.defaultExp = res.data[0];
                }
            });
        })();

        function loadList() {
            $http.get(api.getListsUrl, {
                params: $scope.page
            }).success(function (res) {
                if (res.state) {
                    var data = res.data;
                    appendLists(data.data);
                    $scope.nextPage = data.nextPage
                }
            });
        }


        function appendLists(lists) {
            if (lists.length == 0) {
                return false;
            }

            angular.forEach(lists, function (v) {
                $scope.lists.push(v);
            });
        }

        loadList();
    }]);
})();